package com.projects.android.adapters;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.projects.android.R;
import com.projects.android.dto.RestaurantDTO;
import com.projects.android.utils.ImageDownloader;
import com.projects.android.utils.Utils;

public class RestaurantsAdapter extends ArrayAdapter<RestaurantDTO> {
	private LayoutInflater mInflater;
	private final ImageDownloader imageDownloader = new ImageDownloader();
	
	public RestaurantsAdapter(Context context, int resource,
			int textViewResourceId, List<RestaurantDTO> objects) {
		super(context, resource, textViewResourceId, objects);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RestaurantViewHolder holder = null;
		TextView title = null;
		TextView detail = null;
		ImageView i11 = null;
		RestaurantDTO rowData = getItem(position);

		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.restaurantitem, null);
			holder = new RestaurantViewHolder(convertView);
			convertView.setTag(holder);
		}

		holder = (RestaurantViewHolder) convertView.getTag();

		title = holder.gettitle();
		title.setText(rowData.getName());

		detail = holder.getdetail();
		detail.setText("Business Times: " + rowData.getTimeStart() + " - " + rowData.getTimeEnd());

		i11 = holder.getImage();
		
		int startPoint = Utils.SERVICE_URI.indexOf('.');
		int startDeli = Utils.SERVICE_URI.indexOf('/', startPoint);
		String webUrl = Utils.SERVICE_URI.substring(0, startDeli); 
		String imgUrl = webUrl + "/Content/Images/" + rowData.getImage();
		Log.i("Image URL", imgUrl);
		imageDownloader.download(imgUrl, i11);
		
		return convertView;
	}
}
